{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "090c3369",
   "metadata": {},
   "source": [
    "-------\n",
    "* 2022-9-20 Week05\n",
    "* Python课堂笔记\n",
    "-------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b91198f",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    ">1.有序的数据结构之列表\n",
    "\n",
    ">2.用户输入\n",
    "\n",
    ">3.关于格式化文本信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8d958bad",
   "metadata": {},
   "outputs": [],
   "source": [
    "Seventeen_03_name = \"Joushua\"\n",
    "\n",
    "#思考：一个变量存放一个班的同学名字\n",
    "#思考：处理数据是否麻烦"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "c4c946c4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Joushua,Mingu,Wonwoo\n"
     ]
    }
   ],
   "source": [
    "Seventeen_name_list = \"Joushua,Mingu,Wonwoo\"\n",
    "print(Seventeen_name_list )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "72b9697e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Joushua'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Seventeen_name_list[0:7]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "04751767",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#思考：这个名单有多少人名\n",
    "len(Seventeen_name_list )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19da7866",
   "metadata": {},
   "source": [
    "## 1.引入列表的概念\n",
    "\n",
    ">1.注释：我们已经学习了“数值”和“字符”，list可以放数值和字符，但是通常要么一起放数值，要么一起放字符\n",
    "\n",
    ">总结一些通用的有序数据结构的方法\n",
    "* 1.取值：通过索引值取出对应的内容\n",
    "* 2.切片：通过索引值区间取出对应的一些内容\n",
    "* 3.in可以查看对象是否存在\n",
    "\n",
    ">列表的一些方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3214c542",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "A =[1,2,3,4]\n",
    "print(A,type(A))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "4f7b3bfc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Joushua', 'Mingu', 'Wonwoo'] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "Seventeen_name_list = [\"Joushua\",\"Mingu\",\"Wonwoo\"]\n",
    "print(Seventeen_name_list,type(Seventeen_name_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3eafac10",
   "metadata": {},
   "source": [
    "### 1.1 列表的运算法则（Operation）\n",
    "* <font color=\"red\"> 运算法则不会改变列表数据本身的形态，列表的数据本身不作修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "e1a64bc6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Joushua'"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#取出 Seventeen_name_list第一位成员的名字\n",
    "Seventeen_name_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "598abd99",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#查看Seventeen_name_list有多少位成员\n",
    "len(Seventeen_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2368bac8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Seventeen Joushua\n",
      "Seventeen Mingu\n",
      "Seventeen Wonwoo\n"
     ]
    }
   ],
   "source": [
    "for i in Seventeen_name_list:\n",
    "    print(\"Seventeen\",i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "8ad43de2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#in 可以查询数据是否存在\n",
    "\"YuQ\" in Seventeen_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "127d3b39",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 关于运算法则（in + 切片 取值len(s) s.count)\n",
    "Eightnteen_new_list =[\"xiaoming\",\"lihua\"]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "f7d8e24a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['xiaoming', 'lihua', 'Joushua', 'Mingu', 'Wonwoo']"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Eightnteen_new_list  + Seventeen_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "77cf211c",
   "metadata": {},
   "outputs": [],
   "source": [
    "#思考：一个list 可不可以出现 重复项？\n",
    "网一_age_list = [18,19,20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "16c8754a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 20]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_age_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b979dbd",
   "metadata": {},
   "source": [
    "### 2.2 列表的方法（Function）—对列表数据本身进行操作\n",
    ">1.增\n",
    " >2.删\n",
    " >3.改\n",
    " >4.查"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "71977d24",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Append 增加元素到末尾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "6a6cc058",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Joushua', 'Mingu', 'Wonwoo']"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Seventeen_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "c61e550f",
   "metadata": {},
   "outputs": [],
   "source": [
    "Seventeen_name_list.append(\"Dino\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "14af70e0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Joushua', 'Mingu', 'Wonwoo', 'Dino']"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Seventeen_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "99b1d92e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# insert 增加元素到某个位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "3af05378",
   "metadata": {},
   "outputs": [],
   "source": [
    "Seventeen_name_list.insert(0,\"jeon\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "a3163c6f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jeonchan', 'Joushua', 'Mingu', 'Wonwoo', 'Dino']"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Seventeen_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "dbc6ec20",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jeonchan', 'Joushua', 'Mingu', 'Wonwoo', 'Dino']"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# remove 经常和 in一起使用，先检查成员变量是否存在，然后在remove\n",
    "if \"jeon\"in Seventeen_name_list:\n",
    "    Seventeen_name_list.remove(\"jeon\")\n",
    "Seventeen_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "f00f0b8e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(Seventeen_name_list.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "935b1efe",
   "metadata": {},
   "outputs": [],
   "source": [
    "Seventeen_name_list.remove(\"jeon\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "3ea0cad4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jeonchan', 'Joushua', 'Mingu', 'Wonwoo', 'Dino']"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Seventeen_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "b81b7d32",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 18, 19, 18, 17, 20, 20, 18, 19, 16, 17, 20]"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#请移除19的元素\n",
    "age_list = [18,19,18,19,18,17,20,20,18,19,16,17,20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "id": "45730552",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 18, 18, 17, 20, 20, 18, 16, 17, 20]"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思路1:remove（value）先决条件,知道值出现的次数\n",
    "age_list = [18,19,18,19,18,17,20,20,18,19,16,17,20]\n",
    "#查看删除的变量\n",
    "删除的数量 =age_list.count(19)\n",
    "for i in range(3):\n",
    "    if 19 in age_list:\n",
    "        age_list.remove(19)\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "cfecb813",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_list = [18,19,18,19,18,17,20,20,18,19,16,17,20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "62c1effa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 18, 19, 18, 17, 20, 20, 18, 19, 16, 17, 20]"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "bd25351e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 19\n",
      "2 19\n",
      "7 19\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[18, 18, 18, 17, 20, 20, 18, 16, 17, 20]"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思路2:pop（index），先决条件，知道值出现的位置\n",
    "age_list = [18,19,18,19,18,17,20,20,18,19,16,17,20]\n",
    "for i,v in enumerate(age_list):\n",
    "    if v == 19:\n",
    "        print(i,v)\n",
    "        age_list.pop(i)\n",
    "age_list\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "911592d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "#pop "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "f2e820f2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Dino'"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Seventeen_name_list.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "23618b0f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jeonchan', 'Joushua', 'Mingu', 'Wonwoo']"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Seventeen_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "852fe716",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Mingu'"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Seventeen_name_list.pop(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "746a745f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['jeonchan', 'Joushua', 'Wonwoo']"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Seventeen_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "id": "4c1ae610",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "list.extend() takes exactly one argument (2 given)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[0;32mIn [134]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mage_list\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mextend\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mDino\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mMingyu\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "\u001b[0;31mTypeError\u001b[0m: list.extend() takes exactly one argument (2 given)"
     ]
    }
   ],
   "source": [
    "age_list.extend(\"Dino\",\"Mingyu\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3bf86cb0",
   "metadata": {},
   "outputs": [],
   "source": [
    "#课后练习：课本p68 and p81\n",
    "#剩下的几个方法reserve，extend，copy，clear，sort回去尝试练习\n",
    "#总结列表的方法，用表格的形式写在markdown上"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd1e6868",
   "metadata": {},
   "source": [
    "## 课本P68练习:用列表方法处理列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "98a874c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "phrase = \"don't panic!\"\n",
    "\n",
    "plist = list(\"don't panic!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a79ebf86",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['d', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "don't panic!\n"
     ]
    }
   ],
   "source": [
    "print(plist)\n",
    "print(phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "7704ec1b",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(4):\n",
    "    plist.pop() #执行4次，删除对象\n",
    "plist.pop(0)\n",
    "plist.remove(\"'\")\n",
    "plist.extend([plist.pop(),plist.pop()])#pop最先执行，把后两个字符去掉，接着执行extend\n",
    "#这里的extend相当于append，添加了pop删除掉的后两个字符\n",
    "\n",
    "plist.insert(2,plist.pop(3)) #这里用pop把空格删除，再把控干插入2的位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "d0457fb3",
   "metadata": {},
   "outputs": [],
   "source": [
    "new_phrase = \"\".join(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "91b12cce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "don't panic!\n"
     ]
    }
   ],
   "source": [
    "print(plist)\n",
    "\n",
    "print(phrase)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9cf5211c",
   "metadata": {},
   "source": [
    "## 课本P81练习：用中括号记法处理列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "76d8927e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['d', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "don't panic!\n"
     ]
    }
   ],
   "source": [
    "phrase = \"don't panic!\"\n",
    "\n",
    "plist = list(phrase)\n",
    "\n",
    "print(plist)\n",
    "\n",
    "print(phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "4bce376d",
   "metadata": {},
   "outputs": [],
   "source": [
    "new_phrase = \".join(plist[1:3])\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "22259274",
   "metadata": {},
   "outputs": [],
   "source": [
    "new_phrase = new_phrase+''.join([plist[5],plist[4],plist[7],plist[6]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "8787af3e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['d', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "don't panic!\n"
     ]
    }
   ],
   "source": [
    "print(plist)\n",
    "\n",
    "print(phrase)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a9fd5fcc",
   "metadata": {},
   "source": [
    "### reserve，extend，copy，clear，sort"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f8547321",
   "metadata": {},
   "outputs": [],
   "source": [
    "#extend"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "6c8916c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "num = [1,2,3,4,5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "7d0d303f",
   "metadata": {},
   "outputs": [],
   "source": [
    "num.extend([6,7])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "5a73f603",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 7]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "0d9c387a",
   "metadata": {},
   "outputs": [],
   "source": [
    "#count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "74277a5b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num.count(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea549e60",
   "metadata": {},
   "outputs": [],
   "source": [
    "# reserve"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "fba7ad00",
   "metadata": {},
   "outputs": [],
   "source": [
    "list.reverse(num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "98d87a97",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[7, 6, 5, 4, 3, 2, 1]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "066f1a8d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# sort"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "fb90abb6",
   "metadata": {},
   "outputs": [],
   "source": [
    "list.sort(num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "43651eed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 7]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2d171ce4",
   "metadata": {},
   "outputs": [],
   "source": [
    "#copy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5cb52b46",
   "metadata": {},
   "outputs": [],
   "source": [
    "phrase = num.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "597f9d85",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 7]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "phrase"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f1dbbbac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "phrase.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "0361dd67",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "phrase"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "dd849f8e",
   "metadata": {},
   "outputs": [],
   "source": [
    "#clear"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "c781fc25",
   "metadata": {},
   "outputs": [],
   "source": [
    "num.clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "0a12d18b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "34ca0a62",
   "metadata": {},
   "outputs": [],
   "source": [
    "#index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "29a40c36",
   "metadata": {},
   "outputs": [],
   "source": [
    "num = [1,3,2,4,5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "7d623423",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num.index(4) #检查对象在列表的哪个位置 （索引值）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "90281799",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num.index(3,0) #index括号里有两个参数，第1个为索引内容，第2个为索引开始位置"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b646abcf",
   "metadata": {},
   "source": [
    "# 总结:列表的方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4770a01d",
   "metadata": {},
   "source": [
    "|     方法             |         作用        |\n",
    "|  :---------            | :--------:        |\n",
    "|    extend             | 插入一个列表并合并 |\n",
    "|    insert              |增加元素到某个位置|\n",
    "|     append            |增加元素到末尾|\n",
    "|    remove             |直接指定删除列表中的元素|\n",
    "|    pop                |（1）没有指定索引值，删掉最后一项；（2）指定索引值,删除指定元素|\n",
    "|    reserve|对列表进行倒序排列|\n",
    "|    sort|对列表进行正序排列|\n",
    "|    count|检查某个元素在列表里面的个数|\n",
    "|    clear|删除列表中的所有元素|\n",
    "|    copy|建立现有列表的副本，初始化一个新列表|\n",
    "|    index|检查元素的位置|\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3c0cfc8",
   "metadata": {},
   "source": [
    ">一.insert，extend，append用于扩展列表\n",
    "* 1.extend:插入一个列表并合并\n",
    "* 2.insert:从列表中插进一个对象；注意:需要指定索引值插入对象\n",
    "* 3.append:向列表的尾部添加对象\n",
    ">二.pop，remove收缩列表\n",
    "* 1.remove:直接指定删除列表中的对象\n",
    "* 2.pop:（1）没有指定索引值，删掉最后一项；（2）指定索引值,删除指定对象\n",
    ">三.reserve,sort对列表进行排序\n",
    "* 1.reserve:对列表进行倒序排列\n",
    "* 2.sort:对列表进行正序排列\n",
    ">四.count,clear,copy\n",
    "* 1.count:检查某个对象在列表里面的个数\n",
    "* 2.clear:删除列表中的所有对象\n",
    "* 3.copy:建立现有列表的副本，初始化一个新列表\n",
    ">五.index:检查对象的位置"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a391df67",
   "metadata": {},
   "source": [
    "# 自学内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3d442906",
   "metadata": {},
   "outputs": [],
   "source": [
    "# for循环了解切片  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "2dfaed96",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\t h\n",
      "\t e\n",
      "\t y\n",
      "\t ,\n",
      "\t p\n",
      "\t l\n",
      "\n"
     ]
    }
   ],
   "source": [
    "phrase = \"hey,please teach me how to use the python!\"  #中括号记法，3个切片来抽取和显示一个列表片段\n",
    "letters = list(phrase)\n",
    "\n",
    "for char in letters[:6]:\n",
    "    print('\\t',char)\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "d8c2ac45",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\t\t p\n",
      "\t\t y\n",
      "\t\t t\n",
      "\t\t h\n",
      "\t\t o\n",
      "\t\t n\n",
      "\t\t !\n"
     ]
    }
   ],
   "source": [
    "for char in letters[-7:]:\n",
    "    print('\\t'*2,char)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "6019ed02",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\t\t\t  \n",
      "\t\t\t t\n",
      "\t\t\t e\n",
      "\t\t\t a\n",
      "\t\t\t c\n",
      "\t\t\t h\n",
      "\t\t\t  \n",
      "\t\t\t m\n",
      "\t\t\t e\n",
      "\t\t\t  \n"
     ]
    }
   ],
   "source": [
    "for char in letters[10:20]:\n",
    "    print('\\t'*3,char)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f936169",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
